/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.demo.controller;

import SimilarityWordInSentence.SimilarityWordInSentence;
import TencentAsr.AudioToText;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Controller
public class BasicController
{
    String[] page1Return=new String[]{"家用梯","客梯","观光梯"};
    String[] page2Return=new String[]{"工业风格","现代简约","清新风格","典雅风格","欧式风格","新中式"};
    String[] page3Return=new String[]{"返回首页","天花","地板","扶手"};
    String[] page4Return=new String[]{"ce-011","ce-066","dp-046","lm-220"};
    String[] page5Return=new String[]{"返回首页","天花","地板","扶手"};

    // http://127.0.0.1:8080/hello
    @RequestMapping("/")
    public String hello()
    {
        return "temp";
    }
    @PostMapping(value ="/upload")
    @ResponseBody
    public String[] speechRe(@RequestParam("audio") MultipartFile file,String[] wordList){
        AudioToText audioToText_ =new AudioToText();
        try
        {
            String audioToText= audioToText_.audioToText(file.getBytes());
            if(!Objects.equals(audioToText, ""))
            {
                System.out.println("语音转文字："+audioToText);
                String contain = SimilarityWordInSentence.similarityWordInSentence(audioToText, wordList);
                System.out.println(audioToText+"中含有词语："+contain);
                if(contain!=null)
                {
                    if (contain.equals("ai设计"))
                    {
                        String[] rn = Arrays.copyOf(page1Return, page1Return.length + 1);
                        rn[rn.length - 1] = "ai设计";
                        return rn;
                    }
                    if (contain.equals("家用梯"))
                    {
                        String[] rn = Arrays.copyOf(page2Return, page2Return.length + 1);
                        rn[rn.length - 1] = "家用梯";
                        return rn;
                    }
                    if (contain.equals("现代简约"))
                    {
                        String[] rn = Arrays.copyOf(page3Return, page3Return.length + 1);
                        rn[rn.length - 1] = "现代简约";
                        return rn;
                    }
                    if (contain.equals("天花"))
                    {
                        String[] rn = Arrays.copyOf(page4Return, page4Return.length + 1);
                        rn[rn.length - 1] = "天花";
                        return rn;
                    }
                    if (contain.equals("ce-066"))
                    {
                        String[] rn = Arrays.copyOf(page5Return, page5Return.length + 1);
                        rn[rn.length - 1] = "ce-066";
                        return rn;
                    }
                }
                return null;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


}
